﻿// -------------------------------------------------------------------------
//    @FileName         :    NFQuestModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025/2/8
//    @Email            :    445267987@qq.com
//    @Module           :    NFQuestModule
//
// -------------------------------------------------------------------------

#include "NFQuestModule.h"

#include <NFPlayer.h>
#include <NFServiceQuest.h>
#include <NFSession.h>

#include "NFPacketSendUtil.h"
#include "proto_cs_cmd.nanopb.h"

NFQuestModule::NFQuestModule(NFIPluginManager* p) : NFLogicDynamicModule(p)
{
}

NFQuestModule::~NFQuestModule()
{
}

int NFQuestModule::Awake()
{
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_ACCEPT_QUEST_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_COMPLETE_QUEST_REQ);
    return 0;
}

int NFQuestModule::OnHandleClientMessage(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return proto_ff::ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_CS_ACCEPT_QUEST_REQ:
        {
            iRetCode = ProcessAcceptReq(packet, pSession, req);
            break;
        }
        case E_CS_COMPLETE_QUEST_REQ:
        {
            iRetCode = ProcessCompleteReq(packet, pSession, req);
            break;
        }
        default: NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFQuestModule::ProcessAcceptReq(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    NFPlayer* pPlayer = pSession->GetPlayer();
    CHECK_NULL(0, pPlayer);

    return NFServiceQuest::Accept(pPlayer, req.quest_accept_req().quest_id());
}

int NFQuestModule::ProcessCompleteReq(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& csReq)
{
    CHECK_NULL(0, pSession);
    NFPlayer* pPlayer = pSession->GetPlayer();
    CHECK_NULL(0, pPlayer);
    const proto_ff::Proto_CSQuestCompleteReq& req = csReq.quest_complete_req();

    int iRet = NFServiceQuest::Finish(pPlayer, req.quest_id(), req.selected_item_desc_id(), req.selected_item_quality(), req.selected_item_uuid(), req.next_quest_id());
    if (iRet != 0)
    {
        LOG_ERR(pPlayer->GetRoleID(), iRet, "complete quest failed, quest_id:{}", req.quest_id());
        proto_ff::Proto_CSRsp resp;
        proto_ff::Proto_CSQuestCompleteRsp* complete_resp = resp.mutable_quest_complete_rsp();
        complete_resp->set_quest_id(req.quest_id());
        complete_resp->set_error_code((proto_ff::Proto_CS_ErrorCode)iRet);
        return NFPacketSendUtil::SendToClient(pPlayer, E_CS_COMPLETE_QUEST_RSP, &resp);
    }
    return 0;
}
